clear
set scheme s1color

do set_path.do

global cipdata "${datadir}cip_all_both.dta"
global eme "BRL CLP CNH CNY COP HUF IDR ILS INR KRW MXN MYR PEN PHP PLN RUB THB TRY ZAR"

***** Fig A7: Government CIP and CDS spreads *****

*** I.6. CDS ***
tempfile _cds_usd
use "${datadir}markit/clean_markit_data.dta", clear
keep if currency=="USD"
save `_cds_usd', replace

tempfile _cds
use "${datadir}markit/clean_markit_data.dta", clear
keep if currency=="CNY"
replace currency="CNH"
append using "${datadir}markit/clean_markit_data.dta"

* 3m data is limited; use 6m instead
keep if inlist(tenor,"1y","2y","3y","5y","10y","20y","30y","6m")
replace tenor="3m" if tenor=="6m"
save `_cds', replace

tempfile _cds_can
import excel "${datadir}bloomberg/can_cds_update.xlsx", sheet(data) firstrow clear
gen currency = "CAD"
drop if missing(date)
save `_cds_can', replace

* Combine with CIP
use $cipdata, clear

* Merge with CDS data
mmerge currency tenor date using `_cds', type(1:1) unmatch(master) ukeep(parspread) urename(parspread cds)
drop _merge

mmerge tenor date using `_cds_usd', type(n:1) unmatch(master) ukeep(parspread) urename(parspread cds_usd)
drop _merge

replace cds = cds * 10000 			// convert to bps
replace cds_usd = cds_usd * 10000	// convert to bps

* CDS data from Bloomberg from 11/17/23 when Markit data are not available; lower than Market by ~5-10bps
mmerge currency tenor date using `_cds_can', type(1:1) unmatch(master) uname(_)
replace cds = _cds if _merge==3 & missing(cds) & !missing(_cds)
drop _*

* Calculate CDS Spread
gen quarter = qofd(date)
format quarter %tq
sort currency date

foreach x in cip_govt cds cds_usd {
	gen `x'_mean=`x'
	gen `x'_median=`x'
}

collapse (mean) *mean (median) *median, by(date group tenor)
gen week=wofd(date)
gen cds_spread_mean = cds_mean - cds_usd_mean
gen cds_spread_median = cds_median - cds_usd_median

collapse (mean) *mean (median) *median, by(week group tenor)
format week %tw

twoway (line cip_govt_median week) (line cds_spread_median week) if group=="eme" & tenor=="5y" & dofw(week)>=td(01jan2005), legend(size(small) order(1 "Government CIP Deviation" 2 "CDS Spread")) title("Emerging Markets",  size(medium)) xti("") xla(, labsize(small) format(%twCY) nogrid) ytitle("Basis Points", size(small)) graphregion(lcolor(none) ilcolor(none) fcolor(white) ifcolor(white)) bgcolor(white) plotregion(lcolor(white)) subtitle(, size(small) ring(0) pos(1) nobexpand)note("") legend(region(lstyle(none)) rows(1)) yline(0, lwidth(vthin) lcolor(black)) yla(, labsize(small))
graph export "${outdir}figures/govt_cip_cds_em.eps", replace

twoway (line cip_govt_median week) (line cds_spread_median week) if group=="g10" & tenor=="5y" & dofw(week)>=td(01jan2005), legend(size(small) order(1 "Government CIP Deviation" 2 "CDS Spread")) title("Developed Markets",  size(medium)) xti("") xla(, labsize(small) format(%twCY) nogrid) ytitle("Basis Points", size(small)) graphregion(lcolor(none) ilcolor(none) fcolor(white) ifcolor(white)) bgcolor(white) plotregion(lcolor(white)) subtitle(, size(small) ring(0) pos(1) nobexpand)note("") legend(region(lstyle(none)) rows(1)) yline(0, lwidth(vthin) lcolor(black)) yla(, labsize(small))
graph export "${outdir}figures/govt_cip_cds_dm.eps", replace

